//
//  ViewController.swift
//  RichTextEditor
//
//  Created by 李金梁 on 2020/6/23.
//  Copyright © 2020 李金梁. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    var editorView:RichTextEditorView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        editorView = RichTextEditorView.init(frame: view.bounds)
        self.view.addSubview(editorView)
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "转成html", style: .plain, target: self, action: #selector(to_html))
        
        let filePath = Bundle.main.path(forResource: "editor", ofType: "html")

        do {

            let htmlString = try String.init(contentsOfFile: filePath!, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
            let str = String.init(format: "<head><style>img{max-width:%f !important;height:auto}</style></head>%@", screen_width - 20, htmlString)
            print( str.attributedString())
            editorView.contentTextView.attributedText = str.attributedString()
        }

        catch{

        }

    
    }

    
    @objc func to_html() {
        print(editorView.contentTextView.attributedText.html())
    }

}

